package com.pmsystem.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pmsystem.dao.OfficemanMapper;
import com.pmsystem.pojo.Office;
import com.pmsystem.pojo.Officeman;
import com.pmsystem.pojo.OfficemanExample;
import com.pmsystem.pojo.OfficemanShow;
import com.pmsystem.pojo.OfficemanExample.Criteria;

/**
 * 
* <p>Title: OfficemanService</p>  
* <p>Description:处理办公室人员信息的业务层 </p>  
* @author 树昌  
* @date 2019年1月25日
 */
@Service
public class OfficemanService {
	
	@Autowired
	OfficemanMapper officemanMapper;
	@Autowired
	OfficeService officeService;
	
	
	/**
	 * 
	 * <p>Title: addOfficeman</p>  
	 * <p>Description:添加一条办公室人员信息的记录 </p>  
	 * @author 树昌
	 * @param officeman
	 * @return
	 */
	public boolean addOfficeman(Officeman officeman) {
		int row = officemanMapper.insertSelective(officeman);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: showOfficeman</p>  
	 * <p>Description:显示办公室人员 </p>  
	 * @author 树昌
	 * @param keyword	搜索关键词
	 * @param startRow	页码
	 * @param pageSize	每页显示的记录条数
	 * @return
	 */
	public List<OfficemanShow> showOfficeman(String keyword, Integer startRow, Integer pageSize){
		
		OfficemanExample officemanExample = new OfficemanExample();
		Criteria criteria = officemanExample.createCriteria();
		criteria.andEnableEqualTo((byte) 1);
		// 模糊查询
		if (keyword != null) {
			keyword = "%" + keyword + "%";
			criteria.andOfficemanNameLike(keyword);
			Criteria criteria2 = officemanExample.or();
			criteria2.andOfficemanSexLike(keyword);
			Criteria criteria3 = officemanExample.or();
			criteria3.andJobLike(keyword);
			Criteria criteria4 = officemanExample.or();
			criteria4.andOfficemanTelephoneLike(keyword);
		}
		// 分页查询
		if (startRow != null && pageSize != null) {
			if (((startRow - 1) * pageSize) >= 0 && pageSize >= 1) {
				officemanExample.setStartRow((startRow - 1) * pageSize);
				officemanExample.setPageSize(pageSize);
			} else {
				return null;
			}
		}
		// 没有条件则为全查询
		List<Officeman> listofficeman = officemanMapper.selectByExample(officemanExample);
		List<Office> listoffice = officeService.showOffice(null, null, null);
		List<OfficemanShow> listofficemanshow =new ArrayList<>();
		
		for (Officeman officeman : listofficeman) {
			
			OfficemanShow officemanShow = new OfficemanShow();
			officemanShow.setOfficemanId(officeman.getOfficemanId());
			officemanShow.setOfficemanName(officeman.getOfficemanName());
			officemanShow.setOfficemanSex(officeman.getOfficemanSex());
			officemanShow.setOfficemanAge(officeman.getOfficemanAge());
			officemanShow.setJob(officeman.getJob());
			officemanShow.setOfficemanTelephone(officeman.getOfficemanTelephone());
			
			for (Office office : listoffice) {
				
				if(officeman.getBlgOfficeId()==office.getOfficeAreaId()) {
					officemanShow.setOfficeName(office.getOfficeName());
				}
			}
			
			if (officeman.getBlgSuperId() != null) {
				for (Officeman officeman2 : listofficeman) {
					if (officeman.getBlgSuperId() == officeman2.getOfficemanId()) {
						officemanShow.setBossName(officeman2.getOfficemanName());
					}
				}
			}
			
			listofficemanshow.add(officemanShow);
		}
		return listofficemanshow;
	}
	
	
	/**
	 * 
	 * <p>Title: showOneOfficeman</p>  
	 * <p>Description:根据ID查询一条办公室人员的信息</p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	public Officeman showOneOfficeman(int id) {
		
		Officeman officeman = officemanMapper.selectByPrimaryKey(id);
		return officeman;
		
	}
	
	
	/**
	 * 
	 * <p>Title: deleteOfficeman</p>  
	 * <p>Description:删除一条办公室人员信息的记录 </p>  
	 * @author 树昌
	 * @param officeman
	 * @return
	 */
	public boolean deleteOfficeman(Officeman officeman) {
		
		OfficemanExample officemanExample = new OfficemanExample();
		Criteria criteria = officemanExample.createCriteria();
		criteria.andOfficemanIdEqualTo(officeman.getOfficemanId());
		officeman.setEnable((byte) 0);
		int row = officemanMapper.updateByExampleSelective(officeman, officemanExample);
		
		return row==1?true:false;
	}
	
	/**
	 * 
	 * <p>Title: deleteAllOfficeman</p>  
	 * <p>Description:批量删除办公室人员信息的记录 </p>  
	 * @author 树昌
	 * @param listID
	 * @return
	 */
	public boolean deleteAllOfficeman(List<Integer> listID) {
		
		OfficemanExample officemanExample = new OfficemanExample();
		Criteria criteria = officemanExample.createCriteria();
		criteria.andOfficemanIdIn(listID);
		Officeman officeman = new Officeman();
		officeman.setEnable((byte) 0);
		int row =officemanMapper.updateByExampleSelective(officeman, officemanExample);
		return row==1?true:false;
	}
	
	/**
	 * 
	 * <p>Title: updateOfficeman</p>  
	 * <p>Description:修改一条办公室人员信息的记录 </p>  
	 * @author 树昌
	 * @param officeman
	 * @return
	 */
	public boolean updateOfficeman(Officeman officeman) {
		
		OfficemanExample officemanExample = new OfficemanExample();
		Criteria criteria = officemanExample.createCriteria();
		criteria.andOfficemanIdEqualTo(officeman.getOfficemanId());
		int row = officemanMapper.updateByExampleSelective(officeman, officemanExample);
		return row==1?true:false;
	}
	
}
